<?php
declare (strict_types = 1);

namespace app\controller\v1\api;

use app\BaseController;
use app\common\Send;
use app\service\MemberService;
use think\App;
use think\facade\Config;
use think\Request;
use app\validate\Member as MemberValidate;
use app\service\ConfigService;
class Member extends BaseController
{
    use Send;

    protected $memberService;

    protected $configService;

    public function __construct(App $app, MemberService $memberService,  ConfigService $configService)
    {
        parent::__construct($app);
        $this->memberService = $memberService;
        $this->configService = $configService;
    }


    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
        $data = $request->param();
        (new MemberValidate())->goCheck($data,'add');
        $result = $this->memberService->save($data);
        return self::success($result);
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        $id = request()->_mid;
        $result = $this->memberService->read($id);
        return self::success($result);
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        $data =  $request->param();
        (new MemberValidate())->goCheck($data,'update');
        $id = request()->_mid;
        $this->memberService->update($id, $data);
        return self::success("更新成功");
    }

    public function login()
    {
        $data = $this->request->param();
        $result = $this->memberService->login($data);
        return self::success($result);
    }

    /**
     * 忘记密码功能接口
     *
     * 该方法处理用户忘记密码的请求，通过验证用户提供的信息并调用会员服务中的忘记密码方法来实现密码重置
     * 主要涉及用户输入的参数验证和密码重置的实际执行
     */
    public function forgetPassword(){
        // 获取用户提交的参数
        $data = $this->request->param();
        // 执行数据验证，确保数据符合忘记密码的规则
        (new MemberValidate())->goCheck($data,'forget');
        // 调用会员服务中的忘记密码方法，执行密码重置操作
        $result = $this->memberService->forgetPassword($data);
        // 返回操作结果
        return self::success($result);
    }

    public function logout(){
        $token = request()->_mtoken;
        $this->memberService->logout($token);
        return self::success("退出成功");
    }

    public function getContacts()
    {
        $puser = $this->memberService->getReference();
        $qq = $this->configService->getByKey('qq');//Config::get('wechat.qq');
        return self::success([
            'phone'=>$puser->phone??'',
            'qq'=>$qq
        ]);
    }

    public function getVipInfo()
    {
        $puser = $this->memberService->getVipInfo();
        return self::success($puser);
    }


    public function setPercentage()
    {
        $data = $this->request->param();
        $result = $this->memberService->setPercentage($data);
        return self::success($result);
    }

    public function setProportion()
    {
        $data = $this->request->param();
        $result = $this->memberService->setProportion($data);
        return self::success($result);
    }

    public function getAllTeam()
    {
        $data = $this->request->param();
        $result = $this->memberService->getAllTeam();
        return self::success($result);
    }

}
